package com.click369.mianshiqingbao.service.impl;

import com.click369.mianshiqingbao.mapper.UserBeanMapper;
import com.click369.mianshiqingbao.pojo.UserBean;
import com.click369.mianshiqingbao.pojo.UserBeanExample;
import com.click369.mianshiqingbao.pojo.UserBeanExample.Criteria;
import com.click369.mianshiqingbao.pojo.entity.PageResult;
import com.click369.mianshiqingbao.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户信息的业务访问接口实现类
 */
@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserBeanMapper userBeanMapper;
    /**
     * 条件分页查询用户信息列表
     * @return 分页查询信息的封装类对象
     */
    @Override
    public PageResult findPage(UserBean userbean, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<UserBean> page=(Page<UserBean>)userBeanMapper.selectByExample(null);
        return new PageResult(page.getTotal(),page.getResult());
    }
    /**
     * 增加用户信息
     * @param userbean 用户信息对象
     */
    @Override
    public void add(UserBean userbean) {
        userBeanMapper.insert(userbean);
    }
    /**
     * 根据ID获取用户信息对象
     * @param id 用户信息的id
     * @return 用户信息对象
     */
    @Override
    public UserBean findOne(Long id) {
        return userBeanMapper.selectByPrimaryKey(id);
    }
    /**
     * 修改用户信息
     * @param userbean 用户信息对象
     */
    @Override
        public void update(UserBean userbean) {
        userBeanMapper.updateByPrimaryKey(userbean);
    }
    /**
     * 批量删除用户信息
     * @param ids 批量删除用户信息的id集合
     */
    @Override
    public void delete(Long[] ids) {
        for (long id:ids){
            userBeanMapper.deleteByPrimaryKey(id);
        }

    }

    /**
     * 判断用户登录
     * @param phoneNum
     * @param password
     */
    @Override
    public UserBean login(String phoneNum, String password) {
        UserBeanExample example = new UserBeanExample();
        Criteria criteria = example.createCriteria();
        criteria.andTelNumberEqualTo(phoneNum);
        criteria.andUserPassEqualTo(password);
        List<UserBean> userlist = userBeanMapper.selectByExample(example);
        if(userlist.size()>0){
            return userlist.get(0);
        }else{
            return null;
        }
    }

    @Override
    public boolean phoneNumExist(String phoneNum) {
        UserBeanExample example = new UserBeanExample();
        Criteria criteria = example.createCriteria();
        criteria.andTelNumberEqualTo(phoneNum);
        List<UserBean> userlist = userBeanMapper.selectByExample(example);
        if(userlist.size()>0){
            return false;
        }else{
            return true;
        }
    }

    @Override
    public boolean userNickExist(String userNick) {
        UserBeanExample example = new UserBeanExample();
        Criteria criteria = example.createCriteria();
        criteria.andUserNickEqualTo(userNick);
        List<UserBean> userlist = userBeanMapper.selectByExample(example);
        if(userlist.size()>0){
            return false;
        }else{
            return true;
        }
    }
}
